<template>
	<view class="home">
		<template v-if="tableData.length">
			<view class="signupCard" v-for="(item,idx) of tableData" :key="idx">
				<view class="top">
					<!-- 毕业证样本 -->
					<view class="left">
						<u-image width="344rpx" height="200rpx" :src="item.dmsp"></u-image>
					</view>

					<view class="right">
						<!-- 考试名称 -->
						<view class="name">考试名称:{{item.name}}</view>

						<!-- 毕业证书 -->
						<view class="ccnm">毕业证书:{{item.ccnm}}</view>

						<!-- 考试期数 -->
						<view class="ccnm">考试期数:{{item.podl}}</view>

						<!-- 主办单位 -->
						<view class="ogze">主办单位:{{item.ogze}}</view>
					</view>
				</view>

				<view class="bottom">
					<view class="left">
						<!-- 报名时间 -->
						<view class="emtm">报名时间{{item.star}}</view>

						<!-- 开考时间 -->
						<view class="emtm">开考时间{{item.emtm}}</view>

						<!-- 考试费 -->
						<view class="cost">考试费{{item.cost}}元</view>
					</view>

					<view class="right">
						<!-- 插槽 -->
						<view class="btn">
							<u-button type="success" @click="goDetails(item)" size="mini">查看详情</u-button>
							<u-button type="primary" @click="goSignUp(item)" size="mini" :disabled="filterState(item)">报名</u-button>
						</view>
					</view>
				</view>
			</view>
		</template>

		<template v-else>
			<no-data></no-data>
		</template>

		<u-loadmore :status="status" />

		<wyb-loading ref="loading" />
		
		<u-toast ref="uToast" />

		<!-- 底部菜单栏 -->
		<tab-bar></tab-bar>
	</view>
</template>

<script>
	import tabBar from "@/components/tabBar.vue"
	import signupCard from "@/components/signupCard.vue"
	import {
		list
	} from "@/api/apiList/exam.js"
	import wybLoading from '@/components/wyb-loading/wyb-loading.vue'
	import {
		isSignUp
	} from "@/utils/isSignUp.js"
	import noData from '@/components/noData.vue'
	import {
		getExaminee
	} from "@/api/apiList/user.js";
	import {
		createNamespacedHelpers
	} from 'vuex'
	const {
		mapMutations
	} = createNamespacedHelpers('userInfoModule')
	export default {
		components: {
			tabBar,
			signupCard,
			wybLoading,
			noData
		},
		data() {
			return {
				current: 1, //当前页
				total: 0, //总数
				size: 3, //每页条数
				loading: false,
				tableData: [], //考试数据列表
				status: 'loadmore',
			}
		},
		methods: {
			...mapMutations(["UPDATE_USER_INFO"]),
			async API_list(size, current) {
				let result = {
					page: {
						size,
						current
					}
				}
				return await list(result)
			},
			async API_getExaminee() {
				//获取个人信息接口
				return await getExaminee();
			},
			goDetails(row) {
				// this.$store.commit("details/UPDATE_ROW", JSON.stringify(row))
				uni.navigateTo({
					url: "/pages/details/details?id=" + row.id
				})
			},
			goSignUp(row) {
				this.$store.commit("signUpForm/UPDATE_ROW", JSON.stringify(row))
				uni.navigateTo({
					url: "/pages/signupForm/signupForm?eid=" + row.id
				})

			},
			filterState(obj) { //过滤状态
				let starTime = new Date(obj.star.replace(/-/g, "/")).getTime()//开始日期
				let ddleTime = new Date(obj.ddle.replace(/-/g, "/")).getTime()//结束日期
				let nowTime = new Date().getTime()//现在日期
				let bool = true
				if(starTime < nowTime && nowTime < ddleTime){
					bool = false
				}
				return bool
			}
		},
		async onShow() {
			let res = await this.API_getExaminee()
			this.UPDATE_USER_INFO(res.data.data) //更新数据
		},
		async mounted() {
			this.$refs.loading.showLoading()
			let res = await this.API_list(this.size, this.current)

			let {
				current,
				records,
				total
			} = res.data.data
			this.tableData = records
			this.current = current
			this.total = total
			this.$refs.loading.hideLoading()
			// console.log(this.tableData)
		},
		async onReachBottom() { //上拉加载
			if (this.tableData.length >= this.total) {
				this.status = 'nomore';
				return
			} else {
				this.status = 'loading';
				this.current = ++this.current
				let res = await this.API_list(this.size, this.current)
				let {
					current,
					records
				} = res.data.data
				this.tableData.push(...records)
			}
		},
		async onPullDownRefresh(){
			this.$refs.loading.showLoading()
			let res = await this.API_list(this.size, this.current)
			
			let {
				current,
				records,
				total
			} = res.data.data
			this.tableData = records
			this.current = current
			this.total = total
			this.$refs.loading.hideLoading()
		}
	}
</script>

<style lang="scss" scoped>
	page,
	.home {
		height: 100%;
	}

	.signupCard {
		padding: 20rpx;
		box-sizing: border-box;

		.top {
			display: flex;

			.left {
				margin-right: 10rpx;
			}

			.right {
				display: flex;
				flex-direction: column;
				justify-content: space-around;
			}
		}

		.bottom {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.left {
				display: flex;
				flex-direction: column;
				justify-content: space-around;
			}

		}
	}

	.btn {
		width: 250rpx;
		display: flex;
		justify-content: space-around;
	}
</style>
